package com.apache.bid.service.cacheplugins.ws;

import java.util.List;
import java.util.Map;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.entity.BidApply;
import com.apache.bid.manager.BidApplyManager;
import com.apache.cache.util.Validator;
import com.apache.tools.DateUtils;
import com.apache.tools.StrUtil;

/**
 * 
 * description:  同步竞买人信息
 * @author dyhou 创建时间：2016-6-6
 */
public class BidApplySynPluginImpl implements PluginConnector {

	private BidApplyManager bidApplyManager;

	public Object execute(ParamsVo param) throws Exception {
		String applyIds = String.valueOf(param.getParams("applyIds"));
		ResultEntity entity = new ResultEntity();
		if (Validator.isNotNull(applyIds)) {//同步ID在查业务系统中标的表

		} else {
			List<Map<String, String>> applys = (List<Map<String, String>>) param.getParams("applys");
			if (Validator.isEmpty(applys)) {
				entity.setResult("false");
				entity.setMessage("标的信息为空");
				return entity;
			}
			ParamsVo<BidApply> appLyVo = new ParamsVo<BidApply>();
			for (int i = 0; i < applys.size(); i++) {
				BidApply apply = saveItemInfo(applys.get(i));
				appLyVo.setObj(apply);
				String id = bidApplyManager.saveInfo(appLyVo);
				if (StrUtil.isNotNull(id)) {
					BidCacheFactory.getInstance().getCacheManager("apply").createCacheObject(apply);
				}
			}
		}
		entity.setResult("true");
		entity.setEntity("true");
		entity.setMessage("竞买人信息同步成功");
		return entity;
	}

	private BidApply saveItemInfo(Map<String, String> map) {
		BidApply item = new BidApply();
		item.setApplyId(map.get("applyid"));
		item.setItemId(Validator.getDefaultStr(map.get("itemid"), "0"));
		item.setSessionId(Validator.getDefaultStr(map.get("sessionid"), "0"));
		item.setUserId(map.get("userid"));
		item.setRole(map.get("role"));
		item.setBidUserNo(map.get("biduserno"));
		item.setCreateTime(Validator.getDefaultStr(map.get("createtime"), DateUtils.Now.fmt_yyyyMMdd_HHmmss()));
		item.setUserName(map.get("username"));
		item.setRealName(map.get("realname"));
		item.setApplyName(map.get("realname"));
		item.setProvince(map.get("province"));
		item.setPhone(map.get("phone"));
		item.setMobile(map.get("mobile"));
		item.setCertNo(map.get("idno"));
		item.setEmail(map.get("email"));
		item.setFax(map.get("fax"));
		item.setBailPayMode(map.get("paytype"));
		item.setBank(map.get("bank"));
		item.setBankAccount(map.get("bankaccount"));
		item.setShouldBail(map.get("bail"));
		item.setFactBail(map.get("factbail"));
		item.setFactBailTime(map.get("factbailtime"));
		item.setFactBailAccount(map.get("factbkaccount"));
		item.setBailFinshFlag(map.get("payStatus"));
		item.setStatus("1");
		item.setCompany(map.get("company"));
		item.setAllowBuyNum(map.get("allowBuyNum"));
		item.setApplyWay(map.get("applyWay"));
		item.setSczlTime(map.get("sczlTime"));
		item.setObjName(StrUtil.doNull(map.get("lastlogintime"), "--"));

		return item;
	}

	public void setBidApplyManager(BidApplyManager bidApplyManager) {
		this.bidApplyManager = bidApplyManager;
	}

}
